package com.koocloud.electroplatemanage.auth.mapper;

import java.util.List;
import java.util.Map;

import com.koocloud.electroplatemanage.pojo.Permission;
import com.koocloud.electroplatemanage.pojo.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;






@Mapper
public interface IRoleDao {
	@Insert("insert into role(role_name,remarks) values(#{roleName},#{remarks})")
	@Options(useGeneratedKeys = true,keyProperty ="id",keyColumn = "id")//加入该注解可以保持对象后，查看对象插入id
	Integer addRole(Role role);
	
	@Update("update role set id=#{id},role_name=#{roleName},remarks=#{remarks}  where id=#{id}")
	Integer updateRole(@Param("id")String id,@Param("roleName") String roleName,@Param("remarks")String remarks);
	
	
	@Delete("delete from role where id=#{id}")
	Integer deleteRole(String id);
	@Select("Select * from role where "+ "(case when #{id} is not null and #{id}!= '' then (id like '%${id}%') else (1=1) end) and "+" (case when #{roleName} is not null and #{roleName}!= '' then (role_name like '%${roleName}%') else (1=1) end)")
	List<Map<String, Object>> selectRole(@Param("id")String id,@Param("roleName")String roleName);

	@Select("select id from role where role_name=#{hasRoleList}")
	String selectRoleId(String hasRoleList);
	
	@Select("select *  from role")
	List<Role> selectRoleList();

	@Select("SELECT tp.permission_name FROM permission tp left JOIN  role_permission  trp  on  tp.id=trp.permission_id  left JOIN  role   tr  on  trp.role_id=tr.id    where tr.id=#{id}")
	List<String> selectAuthList(String id);

	@Select("select count(*) from role where role_name=#{roleName}")
	Integer selectRoleByRoleName(Role role);
	
	@Insert("insert into role_permission(role_id,permission_id) values(#{id},#{parseInt})")
	Integer addRolePermission(@Param("id")long id,@Param("parseInt") int parseInt);
	
	@Select("select count(*) from role where id=#{id}")
	Integer selectRoleByRoleId(int id);

	@Delete("delete from role_permission where role_id=#{id}")
	Integer deleteRolePermissionById(int id);

	@Select("select * from role where role_name=#{roleName}")
	List<Map<String, Object>> selectRoleByName(Role role);

	@Select("select * from role where id=#{id}")
	Role selectRoleInfo(Role role);

	@Select("select * from user where id=#{id}")
	Integer selectUserByUserId(int id);

	@Delete("delete from user_role where user_id=#{id}")
	Integer deleteUserRoleById(int id);
	
	@Insert("insert into user_role(user_id,role_id) values(#{id},#{parseInt})")
	Integer addUserRole(@Param("id")int id,@Param("parseInt") int parseInt);

    @Select("SELECT DISTINCT\r\n" + 
    		"	m.id,\r\n" + 
    		"	m.permission_name,\r\n" + 
    		"	m.url,\r\n" + 
    		"	m.icon,\r\n" + 
    		"	m.ORDER,\r\n" + 
    		"	m.parent_id,\r\n" + 
    		"	m.lvl,\r\n" + 
    		"	m.type \r\n" + 
    		"FROM\r\n" + 
    		"	role r\r\n" + 
    		"	INNER JOIN role_permission rm ON r.id = rm.role_id \r\n" + 
    		"	INNER JOIN permission m ON rm.permission_id = m.id \r\n" + 
    		"WHERE\r\n" + 
    		"	r.id=#{id}")
	List<Permission> findTree(String id);

    @Insert("insert into role_permission(role_id,permission_id) values(#{id},#{chilrdId})")
    @Options(useGeneratedKeys = true,keyProperty ="id",keyColumn = "id")//加入该注解可以保持对象后，查看对象插入id
    Integer addRolePermissionTwo(String id, Long chilrdId);

    
    @Select("   SELECT  GROUP_CONCAT(r.role_name) role_list_str FROM user u\r\n" + 
            "    LEFT JOIN user_role ur on u.id=ur.user_id\r\n" + 
            "    LEFT JOIN role r on ur.role_id=r.id\r\n" + 
            "    where u.enable_fag=0\r\n" + 
            "    and u.id=#{id}")
    String selectAllRoleByUserId(long id);

}
